/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import listener.IDownloadListener;
import control.MStockMidlet;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
import model.Company;
import model.ErrorMessage;
import model.Result;
import model.Help;
import model.Region;
import model.Statistic;
import util.StringUtils;

/**
 *
 * @author Kien.Tran
 */
public class ServiceManager {

    public static void getListRegion(MStockMidlet control, final int accent, final IDownloadListener action) {
        String s = "opt=31&accent=" + accent;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        action.completedAction(result);

                    } else {
                        Vector vector = new Vector();
                        String[] item = null;
                        Region reg = null;

                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");

                        for (int i = 0; i < items.length; i++) {
                            item = StringUtils.splitAdvance(items[i].trim(), "::");
                            if ((item != null) && (item.length > 2)) {
                                reg = new Region(Integer.parseInt(item[0].trim()), item[1].trim(), Integer.parseInt(item[2].trim()));
                                vector.addElement(reg);
                            }
                        }
                        action.completedAction(vector);
                    }
                } else {
                    action.completedAction(null);
                }
            }
        })).start();
    }

    public static void getListCompany(MStockMidlet control, final int accent, final IDownloadListener action) {
        String s = "opt=32&accent=" + accent;
        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        action.completedAction(result);

                    } else {
                        Vector vector = new Vector();
                        String[] item = null;
                        Company com = null;

                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");

                        for (int i = 0; i < items.length; i++) {
                            item = StringUtils.splitAdvance(items[i].trim(), "::");
                            if ((item != null) && (item.length > 10)) {
                                com = new Company();

                                com.setId(Integer.parseInt(item[0].trim()));
                                com.setRegionId(Integer.parseInt(item[1].trim()));
                                com.setName(item[2].trim());
                                com.setIsMonday(Integer.parseInt(item[3].trim()));
                                com.setIsTuesday(Integer.parseInt(item[4].trim()));
                                com.setIsWednesday(Integer.parseInt(item[5].trim()));
                                com.setIsThursday(Integer.parseInt(item[6].trim()));
                                com.setIsFriday(Integer.parseInt(item[7].trim()));
                                com.setIsSaturday(Integer.parseInt(item[8].trim()));
                                com.setIsSunday(Integer.parseInt(item[9].trim()));
                                com.setLevels(Integer.parseInt(item[10].trim()));

                                vector.addElement(com);
                            }
                        }
                        action.completedAction(vector);
                    }
                } else {
                    action.completedAction(null);
                }
            }
        })).start();
    }

    public static void getResultTable(MStockMidlet control, final int accent, String company, String date, String service, String appID, String check, final IDownloadListener action) {
        String s = "opt=11&accent=" + accent + "&company=" + company + "&date=" + date + "&service=" + service + "&app=" + appID + "&check=" + check;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        if (action != null) {
                            action.completedAction(result);
                        }
                    } else {
                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");
                        String[] item = StringUtils.splitAdvance(items[0].toString().trim(), "::");

                        if (item.length > 0) {
                            int flag = Integer.parseInt(item[0]);
                            if (flag == 0) {
                                action.completedAction(new String(item[1].trim() + ":" + item[2]));

                            } else if (flag == 1) {
                                if (items.length > 2) {
                                    // items[1]: company info
                                    // items[2 .. n]:  loterries
                                    Result res = null;
                                    Vector vector = new Vector();

                                    // items[1]: company info
                                    item = StringUtils.splitAdvance(items[1], "::");
                                    res = new Result();
                                    if (item.length > 2) {
                                        res.setCompany(item[0].trim(), item[1].trim(), item[2].trim());
                                    } else {
                                        res.setCompany("", "", "");
                                    }
                                    vector.addElement(res);

                                    // items[2 .. n]:  loterries
                                    for (int i = 2; i < items.length; i++) {
                                        item = StringUtils.splitAdvance(items[i], "::");
                                        if ((item != null) && (item.length > 1)) {
                                            res = new Result();
                                            res.setLottery(item[0].trim(), item[1].trim());
                                            vector.addElement(res);
                                        }
                                    }
                                    action.completedAction(vector);

                                } else {
                                    action.completedAction(new ErrorMessage(ErrorMessage.ERROR_UPDATE_DATA));
                                }


                            } else if (flag == 2) {
                                action.completedAction(new Integer(flag));
                            }

                        } else {
                            action.completedAction(null);
                        }
                    }
                } else {
                    if (action != null) {
                        action.completedAction(null);
                    }
                }
            }
        })).start();
    }

    public static void getLiveResultTable(MStockMidlet control, final int accent, String company, String date, String service, String appID, String check, final IDownloadListener action) {
        String s = "opt=12&accent=" + accent + "&company=" + company + "&date=" + date + "&service=" + service + "&app=" + appID + "&check=" + check;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        if (action != null) {
                            action.completedAction(result);
                        }
                    } else {
                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");
                        String[] item = StringUtils.splitAdvance(items[0].toString().trim(), "::");

                        if (item.length > 0) {
                            int flag = Integer.parseInt(item[0]);
                            if (flag == 0) {
                                action.completedAction(new String(item[1].trim() + ":" + item[2]));

                            } else if (flag == 1) {
                                if (items.length > 2) {
                                    // items[1]: company info
                                    // items[2 .. n]:  loterries
                                    Result res = null;
                                    Vector vector = new Vector();

                                    // items[1]: company info
                                    item = StringUtils.splitAdvance(items[1], "::");
                                    res = new Result();
                                    if (item.length > 2) {
                                        res.setCompany(item[0].trim(), item[1].trim(), item[2].trim());
                                    } else {
                                        res.setCompany("", "", "");
                                    }
                                    if (item.length > 3) {
                                        if (item[3].trim().equals("1")) {
                                            res.setFully(true);
                                        } else {
                                            res.setFully(false);
                                        }
                                    } else {
                                        res.setFully(false);
                                    }
                                    vector.addElement(res);

                                    // items[2 .. n]:  loterries
                                    for (int i = 2; i < items.length; i++) {
                                        item = StringUtils.splitAdvance(items[i], "::");
                                        if ((item != null) && (item.length > 1)) {
                                            res = new Result();
                                            res.setLottery(item[0].trim(), item[1].trim());
                                            vector.addElement(res);
                                        }
                                    }
                                    action.completedAction(vector);

                                } else {
                                    action.completedAction(new ErrorMessage(ErrorMessage.ERROR_UPDATE_DATA));
                                }


                            } else if (flag == 2) {
                                action.completedAction(new Integer(flag));
                            }

                        } else {
                            action.completedAction(null);
                        }
                    }
                } else {
                    if (action != null) {
                        action.completedAction(null);
                    }
                }
            }
        })).start();
    }

    public static void getQuickSearch(MStockMidlet control, final int accent, String company, String date, String result, String service, String appID, String check, final IDownloadListener action) {
        String s = "opt=21&accent=" + accent + "&company=" + company + "&date=" + date + "&result=" + result + "&service=" + service + "&app=" + appID + "&check=" + check;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        if (action != null) {
                            action.completedAction(result);
                        }
                    } else {
                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");
                        String[] item = StringUtils.splitAdvance(items[0].toString().trim(), "::");

                        if (item.length > 0) {
                            int flag = Integer.parseInt(item[0]);
                            if (flag == 0) {
                                action.completedAction(new String(item[1].trim() + ":" + item[2]));

                            } else if (flag == 1) {
                                if (items.length > 2) {
                                    // items[1]: company info
                                    // items[2 .. n]:  loterries
                                    Result res = null;
                                    Vector vector = new Vector();

                                    // items[1]: search result info
                                    item = StringUtils.splitAdvance(items[1], "::");
                                    res = new Result();
                                    if (item.length > 3) {
                                        res.setSearch(item[0].trim(), item[1].trim(), item[2].trim(), item[3].trim());
                                    } else {
                                        res.setSearch("", "", "", "");
                                    }
                                    vector.addElement(res);

                                    // items[2 .. n]:  loterries
                                    for (int i = 2; i < items.length; i++) {
                                        item = StringUtils.splitAdvance(items[i], "::");
                                        if ((item != null) && (item.length > 1)) {
                                            res = new Result();
                                            res.setLottery(item[0].trim(), item[1].trim());
                                            vector.addElement(res);
                                        }
                                    }
                                    action.completedAction(vector);

                                } else {
                                    action.completedAction(new ErrorMessage(ErrorMessage.ERROR_UPDATE_DATA));
                                }


                            } else if (flag == 2) {
                                action.completedAction(new Integer(flag));
                            }

                        } else {
                            action.completedAction(null);
                        }
                    }
                } else {
                    if (action != null) {
                        action.completedAction(null);
                    }
                }
            }
        })).start();
    }

    public static void getStatisticAndAnalyst(MStockMidlet control, final int accent, final IDownloadListener action) {
        String s = "opt=51&accent=" + accent;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        action.completedAction(result);

                    } else {
                        Vector vector = new Vector();
                        String[] item = null;
                        Statistic model = null;

                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");

                        for (int i = 0; i < items.length; i++) {
                            item = StringUtils.splitAdvance(items[i].trim(), "::");
                            if ((item != null) && (item.length > 2)) {
                                model = new Statistic(Integer.parseInt(item[0].trim()), item[1].trim(), Integer.parseInt(item[2].trim()));
                                vector.addElement(model);
                            }
                        }
                        action.completedAction(vector);
                    }
                } else {
                    action.completedAction(null);
                }
            }
        })).start();
    }

    public static void getAnalystResult(MStockMidlet control, final int accent, String company, String startDate, String endDate, String result, String service, String appID, String check, final IDownloadListener action) {
        String s = "opt=52&analysis=1&accent=" + accent + "&company=" + company + "&startdate=" + startDate + "&enddate=" + endDate + "&result=" + result + "&service=" + service + "&app=" + appID + "&check=" + check;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        if (action != null) {
                            action.completedAction(result);
                        }
                    } else {
                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");
                        String[] item = StringUtils.splitAdvance(items[0].toString().trim(), "::");

                        if (item.length > 0) {
                            int flag = Integer.parseInt(item[0]);
                            if (flag == 0) {
                                action.completedAction(new String(item[1].trim() + ":" + item[2]));

                            } else if (flag == 1) {
                                if (items.length > 1) {
                                    // items[1 .. n]: result (string)
                                    Vector vector = new Vector();

                                    for (int i = 1; i < items.length; i++) {
                                        if (items[i].trim().length() > 0) {
                                            vector.addElement(items[i].trim());
                                        }
                                    }

                                    action.completedAction(vector);

                                } else {
                                    action.completedAction(new ErrorMessage(ErrorMessage.ERROR_UPDATE_DATA));
                                }


                            } else if (flag == 2) {
                                action.completedAction(new Integer(flag));
                            }

                        } else {
                            action.completedAction(null);
                        }
                    }
                } else {
                    if (action != null) {
                        action.completedAction(null);
                    }
                }
            }
        })).start();
    }

    public static void getStatisticResult(MStockMidlet control, final int accent, String company, String startDate, String endDate, String service, String appID, String check, final IDownloadListener action) {
        String s = "opt=52&analysis=2&accent=" + accent + "&company=" + company + "&startdate=" + startDate + "&enddate=" + endDate + "&service=" + service + "&app=" + appID + "&check=" + check;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        if (action != null) {
                            action.completedAction(result);
                        }
                    } else {
                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");
                        String[] item = StringUtils.splitAdvance(items[0].toString().trim(), "::");

                        if (item.length > 0) {
                            int flag = Integer.parseInt(item[0]);
                            if (flag == 0) {
                                action.completedAction(new String(item[1].trim() + ":" + item[2]));

                            } else if (flag == 1) {
                                if (items.length > 1) {
                                    // items[1 .. n]: result (string)
                                    Vector vector = new Vector();

                                    for (int i = 1; i < items.length; i++) {
                                        if (items[i].trim().length() > 0) {
                                            vector.addElement(items[i].trim());
                                        }
                                    }

                                    action.completedAction(vector);

                                } else {
                                    action.completedAction(new ErrorMessage(ErrorMessage.ERROR_UPDATE_DATA));
                                }


                            } else if (flag == 2) {
                                action.completedAction(new Integer(flag));
                            }

                        } else {
                            action.completedAction(null);
                        }
                    }
                } else {
                    if (action != null) {
                        action.completedAction(null);
                    }
                }
            }
        })).start();
    }

    public static void getPredictedResult(MStockMidlet control, final int accent, String company, String result, String service, String appID, String check, final IDownloadListener action) {
        String s = "opt=52&analysis=3&accent=" + accent + "&company=" + company + "&result=" + result + "&service=" + service + "&app=" + appID + "&check=" + check;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        if (action != null) {
                            action.completedAction(result);
                        }
                    } else {
                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");
                        String[] item = StringUtils.splitAdvance(items[0].toString().trim(), "::");

                        if (item.length > 0) {
                            int flag = Integer.parseInt(item[0]);
                            if (flag == 0) {
                                action.completedAction(new String(item[1].trim() + ":" + item[2]));

                            } else if (flag == 1) {
                                if (items.length > 1) {
                                    // items[1 .. n]: result (string)
                                    Vector vector = new Vector();

                                    for (int i = 1; i < items.length; i++) {
                                        if (items[i].trim().length() > 0) {
                                            vector.addElement(items[i].trim());
                                        }
                                    }

                                    action.completedAction(vector);

                                } else {
                                    action.completedAction(new ErrorMessage(ErrorMessage.ERROR_UPDATE_DATA));
                                }


                            } else if (flag == 2) {
                                action.completedAction(new Integer(flag));
                            }

                        } else {
                            action.completedAction(null);
                        }
                    }
                } else {
                    if (action != null) {
                        action.completedAction(null);
                    }
                }
            }
        })).start();
    }

    public static void getOtherAnalystResult(MStockMidlet control, final int accent, String analysis, String service, String appID, String check, final IDownloadListener action) {
        String s = "opt=52&accent=" + accent + "&analysis=" + analysis + "&service=" + service + "&app=" + appID + "&check=" + check;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        if (action != null) {
                            action.completedAction(result);
                        }
                    } else {
                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");
                        String[] item = StringUtils.splitAdvance(items[0].toString().trim(), "::");

                        if (item.length > 0) {
                            int flag = Integer.parseInt(item[0]);
                            if (flag == 0) {
                                action.completedAction(new String(item[1].trim() + ":" + item[2]));

                            } else if (flag == 1) {
                                if (items.length > 1) {
                                    // items[1 .. n]: result (string)
                                    Vector vector = new Vector();

                                    for (int i = 1; i < items.length; i++) {
                                        if (items[i].trim().length() > 0) {
                                            vector.addElement(items[i].trim());
                                        }
                                    }

                                    action.completedAction(vector);

                                } else {
                                    action.completedAction(new ErrorMessage(ErrorMessage.ERROR_UPDATE_DATA));
                                }


                            } else if (flag == 2) {
                                action.completedAction(new Integer(flag));
                            }

                        } else {
                            action.completedAction(null);
                        }
                    }
                } else {
                    if (action != null) {
                        action.completedAction(null);
                    }
                }
            }
        })).start();
    }

    public static void getHelps(MStockMidlet control, final int accent, final IDownloadListener action) {
        String s = "opt=41&accent=" + accent;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        action.completedAction(result);

                    } else {
                        Vector vector = new Vector();
                        String[] item = null;
                        Help help = null;

                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");

                        for (int i = 0; i < items.length; i++) {
                            item = StringUtils.splitAdvance(items[i].trim(), "::");
                            if ((item != null) && (item.length > 2)) {
                                help = new Help(Integer.parseInt(item[0].trim()), item[1].trim(), Integer.parseInt(item[2].trim()));
                                vector.addElement(help);
                            }
                        }
                        action.completedAction(vector);
                    }
                } else {
                    action.completedAction(null);
                }
            }
        })).start();
    }

    public static void getHelpDetail(MStockMidlet control, final int accent, String id, final IDownloadListener action) {
        String s = "opt=42&accent=" + accent + "&id=" + id;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        action.completedAction(result);

                    } else {
                        Vector vector = new Vector();
                        String[] item = null;
                        Help help = null;

                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");
                        if (items.length > 0) {
                            for (int i = 0; i < items.length; i++) {
                                item = StringUtils.splitAdvance(items[i], "::");
                                if ((item != null)) {
                                    if (item.length == 1) {
                                        help = new Help(i, "", item[0].trim());
                                    } else {
                                        help = new Help(i, item[0].trim(), item[1].trim());
                                    }
                                    vector.addElement(help);
                                }
                            }
                        }

                        action.completedAction(vector);
                    }
                } else {
                    action.completedAction(null);
                }
            }
        })).start();
    }

    public static void getAboutUs(MStockMidlet control, final int accent, String service, final IDownloadListener action) {
        String s = "opt=61&accent=" + accent + "&service=" + service;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        action.completedAction(result);

                    } else {
                        Vector vector = new Vector();
                        String[] item = null;
                        Help help = null;

                        String[] items = StringUtils.splitAdvance(result.toString().trim(), "$$");
                        if (items.length > 0) {
                            for (int i = 0; i < items.length; i++) {
                                item = StringUtils.splitAdvance(items[i], "::");
                                if ((item != null)) {
                                    if (item.length == 1) {
                                        help = new Help(i, "", item[0].trim());
                                    } else {
                                        help = new Help(i, item[0].trim(), item[1].trim());
                                    }
                                    vector.addElement(help);
                                }
                            }
                        }

                        action.completedAction(vector);
                    }
                } else {
                    action.completedAction(null);
                }
            }
        })).start();
    }

    public static void getIntroduction(MStockMidlet control, final int accent, String service, final IDownloadListener action) {
        String s = "opt=71&accent=" + accent + "&service=" + service;

        new Thread(new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        action.completedAction(result);

                    } else {
                        Vector vector = new Vector();
                        Help help = new Help(0, "", result.toString().trim());
                        vector.addElement(help);

                        action.completedAction(vector);
                    }
                } else {
                    action.completedAction(null);
                }
            }
        })).start();
    }

    public static void checkExpired(MStockMidlet control, final int accent, String service, String appID, String reinstall, final IDownloadListener action) {
        String s = "";
        if (reinstall != null) {
            s = "opt=03&service=" + service + "&app=" + appID + "&time=" + reinstall;
        } else {
            s = "opt=02&service=" + service + "&app=" + appID;
        }

        DataLoader loader = new DataLoader(s, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        if (action != null) {
                            action.completedAction(result);
                        }
                    } else {
                        String[] array = StringUtils.splitAdvance(result.toString().trim(), "::");
                        action.completedAction(array);
                    }
                } else {
                    if (action != null) {
                        action.completedAction(null);
                    }
                }
            }
        });

        loader.loadDataWithoutThread();
    }

    public static void checkService(MStockMidlet control, final int accent, String service, final IDownloadListener action) {
        DataLoader loader = new DataLoader("opt=01&service=" + service, accent, control, new IDownloadListener() {

            public void completedAction(Object result) {
                if (result != null) {
                    if (result instanceof ErrorMessage) {
                        if (action != null) {
                            action.completedAction(result);
                        }
                    } else {
                        String[] array = StringUtils.splitAdvance(result.toString().trim(), "$$");
                        action.completedAction(array);
                    }
                } else {
                    if (action != null) {
                        action.completedAction(null);
                    }
                }
            }
        });

        loader.loadDataWithoutThread();
    }

    public static void sendMessage(final String appID, final String reinstallTime, final String days, final IDownloadListener action) {
        new Thread() {

            public void run() {
                MessageConnection conn = null;
                String address = "sms://:5000";
                String phoneNumber = "8040";
                String space = " ";
                try {
                    conn = (MessageConnection) Connector.open(address);
                    TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
                    msg.setAddress("sms://" + phoneNumber);
                    msg.setAddress(address);
                    msg.setPayloadText("TCTG" + space + appID + space + reinstallTime + space + days);
                    conn.send(msg);

                    if (action != null) {
                        action.completedAction(new String(""));
                    }

                } catch (IOException ioe) {
                    if (action != null) {
                        action.completedAction(null);
                    }

                } finally {
                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (IOException ioe) {
                            ioe.printStackTrace();
                        }
                    }
                }
            }
        }.start();
    }

    public static void sendSMS(final String phoneNumber, final String message, final IDownloadListener action) {
        new Thread() {

            public void run() {
                MessageConnection conn = null;
                String address = "sms://:5000";
                try {
                    conn = (MessageConnection) Connector.open(address);

                    TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
                    msg.setAddress("sms://" + phoneNumber);
                    msg.setPayloadText(message);

                    conn.send(msg);

                    if (action != null) {
                        action.completedAction(new String(""));
                    }

                } catch (IOException ioe) {
                    if (action != null) {
                        action.completedAction(null);
                    }

                } finally {
                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (IOException ioe) {
                            ioe.printStackTrace();
                        }
                    }
                }
            }
        }.start();
    }
}
